package edu.xmut.kakamusic.fragment.chart;

import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.ValueFormatter;

import java.util.ArrayList;

import edu.xmut.kakamusic.R;
import edu.xmut.kakamusic.base.BaseFragment;
import edu.xmut.kakamusic.viewmodel.BarViewModel;

public class BarFragment extends BaseFragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_bar, container, false);
        BarChart chart = root.findViewById(R.id.barChart);
        BarViewModel barViewModel = new ViewModelProvider(this).get(BarViewModel.class);
        barViewModel.getBarList().observe(getViewLifecycleOwner(), bars -> {
            ArrayList<BarEntry> entries1 = new ArrayList<>();
            ArrayList<BarEntry> entries2 = new ArrayList<>();
            for (int i = 0; i < bars.size(); i++) {
                entries1.add(new BarEntry(i, bars.get(i).getLine1().getSalaries()));
                entries2.add(new BarEntry(i, bars.get(i).getLine2().getSalaries()));
            }
            BarDataSet barDataSet1 = new BarDataSet(entries1, "Java工程");
            barDataSet1.setColor(Color.parseColor("#C4FE8B"));
            barDataSet1.setValueTextColor(Color.parseColor("#FE8D9B"));
            barDataSet1.setValueTextSize(10f);
            BarDataSet barDataSet2 = new BarDataSet(entries2, "PHP工程");
            barDataSet2.setColor(Color.parseColor("#FEF68B"));
            barDataSet2.setValueTextColor(Color.parseColor("#FFBB86FC"));
            barDataSet2.setValueTextSize(10f);
            BarData barData = new BarData(barDataSet1, barDataSet2);
            barData.setBarWidth(0.45f);
            chart.setData(barData);
            chart.groupBars(-0.5f, 0.04f, 0.03f);
            chart.invalidate();
            XAxis xAxis = chart.getXAxis();
            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
            xAxis.setTextColor(Color.BLACK);
            xAxis.setAxisLineColor(Color.BLACK);
            xAxis.setAxisLineWidth(3f);
            xAxis.enableGridDashedLine(10f, 10f, 0);
            xAxis.setValueFormatter(new ValueFormatter() {
                @Override
                public String getFormattedValue(float value) {
                    return bars.get((int) value).getLine1().getYear();
                }
            });
            chart.getAxisRight().setEnabled(false);
            YAxis yAxis = chart.getAxisLeft();
            yAxis.setTextColor(Color.BLACK);
            yAxis.setAxisLineColor(Color.BLACK);
            yAxis.setAxisLineWidth(3f);
            yAxis.setSpaceTop(38.2f);
            yAxis.setAxisMinimum(0f);
            yAxis.enableGridDashedLine(10f, 10f, 0);
            LimitLine limitLine = new LimitLine(10000, "厦门十平均工资");
            limitLine.setLineColor(Color.MAGENTA);
            limitLine.setLineWidth(2f);
            yAxis.addLimitLine(limitLine);
            Legend l = chart.getLegend();
            l.setFormSize(10f);
            l.setForm(Legend.LegendForm.CIRCLE);
            l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
            l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
            Description description = chart.getDescription();
            description.setText("Java工程师经验与工资对应情况");
            description.setTextAlign(Paint.Align.CENTER);
            description.setPosition(540f, 100f);
            description.setTextSize(16f);
            description.setTextColor(Color.BLUE);
            chart.animateY(5000);
        });
        return root;
    }
}
